<template>
	<view class="container">
		<u--form ref="queryForm" :model="query" labelWidth="140rpx">
			<u-form-item label="标准箱码" prop="containerBarCode">
				<u--input 
					placeholder="请输入/扫描" 
					v-model.trim="query.containerBarCode" 
					suffixIcon="scan" 
					suffixIconStyle="font-size: 68rpx"
					style="flex: 1;"
				>
				</u--input>
			</u-form-item>
			<u-form-item label="ReelId">
				<u--input 
					placeholder="请输入/扫描" 
					v-model.trim="query.reelId" 
					suffixIcon="scan" 
					suffixIconStyle="font-size: 68rpx"
					style="flex: 1;"
					@confirm="extraInfo"
				>
				</u--input>
			</u-form-item>
		</u--form>
		<!-- 提示组件 -->
		<u-toast ref="uToast"></u-toast>
		<view class="commonButton">
			<u-button type="primary" :disabled="!query.containerBarCode || Object.keys(scanMaterial).length === 0" @click="submit">确认</u-button>
		</view>
	</view>
</template>

<script>
	import { splitMaterialReShelfAPI } from '@/api/work/outbound'
	export default {
		data() {
			return {
				query: {
					containerBarCode: '',
					reelId: ''
				},
				scanMaterial: {}
			};
		},
		methods: {
			async submit() {
				const submitForm = {
					containerCode: this.query.containerBarCode,
					materialReelId: this.query.reelId
				}
				const res = await splitMaterialReShelfAPI(submitForm)
				if (res.code === 200) {
					this.$refs.uToast.show({
						type: 'warning',
						message: res.message
					})
					this.query = {
						containerBarCode: '',
						reelId: ''
					}
					this.scanMaterial = {}
				} else {
					return this.$refs.uToast.show({
						type: 'warning',
						message: res.message
					})
				}
			},
			extraInfo() {
				if (!this.query.reelId.includes('*')) {
					return this.$refs.uToast.show({
						type: 'warning',
						message: '请扫描正确的物料二维码'
					})
				}
				const array = this.query.reelId.split('*')
				if (array.length !== 6) {
					this.query.reelId = ''
					return this.$refs.uToast.show({
						type: 'warning',
						message: '请扫描正确的物料二维码'
					})
				}
				// 物料码 批次 日期 供应商 数量 reelId
				this.scanMaterial = {
					materialCode: array[0],
					materialBatch: array[1],
					date: array[2],
					supplier: array[3],
					materialQty: Number(array[4]),
					reelId: array[5]
				}
				this.query.reelId = this.scanMaterial.reelId
			}
		}
	}
</script>

<style lang="scss">
.commonButton {
	width: 100%;
	display: flex;
	gap: 20rpx;
	position: fixed;
	left: 0;
	bottom: 0;
	box-sizing: border-box;
	padding: 15rpx;
	justify-content: space-between;
}
</style>
